﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李刚
    //
    // 文件名：RegisterGoods.aspx.cs
    // 文件功能描述：医疗机构自行添加产品
    // 创建标识：李刚 2009-07-17
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Collections.Generic;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class RegisterGoods : BasePage
    {
        /// <summary>
        /// 
        /// </summary>
        private string CustomsId = string.Empty;

        public RegisterGoods()
        {
            this.ModuleNo = Module.产品库管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                this.CustomsId = Request.QueryString["ID"];
            }

            if (!Page.IsPostBack)
            {
                //绑定下拉框
                BindSortList();

                //修改状态下 绑定数据
                BindData();
            }
        }

        /// <summary>
        /// 绑定产品分类下拉框
        /// </summary>
        private void BindSortList()
        {
            IBLL_Sort ibbs = Factory.IBLL_Sort();
            IList<Sort> Lsort = ibbs.GetAllSort(null);

            this.dplSort.DataTextField = "SortName";
            this.dplSort.DataValueField = "SortID";
            this.dplSort.DataSource = Lsort;
            this.dplSort.DataBind();

            ListItem item = new ListItem();
            item.Text = "==请选择产品分类==";
            item.Value = Constants.ALL_ITEM_VALUE;
            dplSort.Items.Insert(0, item);
        }
        /// <summary>
        /// 绑定质量层次下拉框
        /// </summary>
        //private void BindLevelList()
        //{
        //    int totalRecord;
        //    BLL.BLL_Common commonCtr = new HZ.DrugTrade.BLL.BLL_Common();
        //    DataTable table = commonCtr.GetAllQualityLevel(1, 0, out totalRecord);

        //    this.dplLevel.DataTextField = "LevellName";
        //    this.dplLevel.DataValueField = "LevelID";
        //    this.dplLevel.DataSource = table;
        //    this.dplLevel.DataBind();

        //    ListItem item = new ListItem();
        //    item.Text = "==请选择质量层次==";
        //    item.Value = Constants.ALL_ITEM_VALUE;
        //    dplLevel.Items.Insert(0, item);

        //}

        /// <summary>
        /// 绑定页面数据，仅限修改状态时
        /// </summary>
        private void BindData()
        {
            if (this.CustomsId == string.Empty)
            {
                //设置窗体为新增状态
                this.tbAdd.Visible = false;
            }
            else
            {
                this.tbAdd.Visible = true;

                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                CustomGoods model = cstCtrl.GetCustomGoodsByID(CustomsId);
                if (model != null)
                {
                    if (model.AuditStatus == 3 && model.AuditStatusMS == 3) //3 表示审核已通过
                    {
                        PageError("产品已审核通过，无法修改！", ReturnUrl);
                    }
                    else
                    {
                        this.txtGoodsName.Text = model.GoodsName;
                        this.txtOutlookc.Text = model.Outlookc;
                        this.txtMaterial.Text = model.Material;
                        this.txtUnit.Text = model.Unit;
                        this.txtPackUnit.Text = model.PackUnit;
                        this.txtCompanyName.Text = model.CompanyName;
                        this.HidCompanyName.Value = model.CompanyName;
                        if (this.dplSort.Items.FindByValue(model.SortID) != null)
                        {
                            this.dplSort.Items.FindByValue(model.SortID).Selected = true;
                        }
                        if (this.dplLevel.Items.FindByValue(model.QualityLevel) != null)
                        {
                            this.dplLevel.Items.FindByValue(model.QualityLevel).Selected = true;
                        }
                        this.txtLimitPrice.Text = model.LimitPrice.Value.ToString("F"); ;
                        //this.txtNote.Text = model.Note;

                        this.txtNotes.Text = model.AuditNote;
                        this.txtNotesMS.Text = model.AuditNoteMS;

                        this.txtStatus.Text = FormatAuditStatus(model.AuditStatus);
                        this.txtStatusMS.Text = FormatAuditStatus(model.AuditStatusMS);

                        //if (model.SubmitToUS == 1)
                        //{
                        //    this.ckbSubmit.Checked = true;
                        //}
                        //else
                        //{
                        //    this.ckbSubmit.Checked = false;
                        //}
                        
                    }
                }
                else
                {
                    PageError("产品信息不存在！", ReturnUrl);
                }
                //
                this.btnSave.Text = "修改";
            }

        }
        /// <summary>
        /// 格式化审核状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string FormatAuditStatus(object obj)
        {
            string status = "未知";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 1: status = "未审核";
                        break;
                    case 2: status = "需修改";
                        break;
                    case 3: status = "已通过";
                        break;
                    case 4: status = "已拒绝";
                        break;
                }
            }
            return status;
        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtGoodsName.Text.Trim()) || string.IsNullOrEmpty(txtOutlookc.Text.Trim()) || string.IsNullOrEmpty(HidCompanyName.Value.Trim()) || string.IsNullOrEmpty(txtLimitPrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("请填写必填项！", this);
                return;
            }

            if (this.CustomsId == string.Empty)
            {

                CustomGoods model = new CustomGoods();
                model.HospitalID = CurrentAccount.GetUser().Org.OrgID; //设置当前登录用户的医疗机构ID
                model.HospitalName = CurrentAccount.GetUser().Org.OrgName;
                model.GoodsName = txtGoodsName.Text.Trim();
                model.Outlookc = txtOutlookc.Text.Trim();
                model.Material = txtMaterial.Text.Trim();
                model.Unit = txtUnit.Text.Trim();
                model.PackUnit = txtPackUnit.Text.Trim();
                model.CompanyName = HidCompanyName.Value.Trim();
                model.SortID = dplSort.SelectedValue;
                model.QualityLevel = dplLevel.SelectedValue;
                model.LimitPrice = string.IsNullOrEmpty(txtLimitPrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtLimitPrice.Text.Trim()).ToDecimal;
               // model.Note = txtNote.Text.Trim();
                model.GovermentPrice = ddlGovermentPrice.SelectedValue.ToString();
                model.SMMPrice = string.IsNullOrEmpty(txtSMPrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtSMPrice.Text.Trim()).ToDecimal;
                model.PriceSource = txtPrcieSource.Text.Trim();

                //if (this.ckbSubmit.Checked)
                //{
                //    model.SubmitToUS = 1;
                //}
                //else
                //{
                //    model.SubmitToUS =0;
                //}
                model.SubmitToUS = 1;//默认提交

                model.AddAreaId = CurrentAccount.GetUser().Org.Area.AreaID.Substring(0, 4) + "00";
                model.AllowHospitalIDS = CurrentAccount.GetUser().Org.OrgID + "、";

                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                int result = cstCtrl.AddCustomGoods(model);
                if (result == 1)
                {
                    PageInfo("新产品申报成功！请等待审核。", "MyGoodsList.aspx");
                }
                else
                {
                    PageError("新产品添加失败，已有重复产品。", "");
                }
            }
            else
            {
                CustomGoods model = new CustomGoods()
                {
                    ID = Convert.ToInt32(this.CustomsId),
                    AuditNote = " ",
                    AddTime = DateTime.Now,
                    SubmitToUS=1,//表示已提交
                    AuditStatusMS=1,//医疗机构编辑之后 重置审核状态
                    AuditStatus = 1,//医疗机构编辑之后 重置中心审核状态
                    HospitalID = CurrentAccount.GetUser().Org.OrgID,//设置当前登录用户的医疗机构ID
                    HospitalName = CurrentAccount.GetUser().Org.OrgName,
                    GoodsName = txtGoodsName.Text.Trim(),
                    Outlookc = txtOutlookc.Text.Trim(),
                    Material = txtMaterial.Text.Trim(),
                    Unit = txtUnit.Text.Trim(),
                    PackUnit = txtPackUnit.Text.Trim(),
                    CompanyName = txtCompanyName.Text.Trim(),
                    SortID = dplSort.SelectedValue,
                    QualityLevel = dplLevel.SelectedValue,
                    LimitPrice = string.IsNullOrEmpty(txtLimitPrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtLimitPrice.Text.Trim()).ToDecimal,
                    //Note = txtNote.Text.Trim()
                };
                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                int result = cstCtrl.UpdateCustomGoods(model);
                {
                    if (result == 1)
                    {
                        PageInfo("产品更新成功！请等待审核。", "MyGoodsList.aspx");
                    }
                    else
                    {
                        PageError("产品更新失败，已有重复产品。", "");
                    }
                }
            }

        }
    }
}
